package com.tro.common.util.upload;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.web.multipart.MultipartFile;



/**
 * 
* @ClassName: KindEditorUpload
* @Description: TODO 文件上传类
* @author leeko
* @date 2017年7月18日 上午10:47:57
*
 */
public class UploadUtil {
	
	/**
	 * 
	* @Title: imageUpload 
	* @Description: TODO(其实就是个文件上传) 
	* @param @param request
	* @param @param savePath
	* @param @param fileList
	* @param @return    设定文件 
	* @return R    返回类型 
	* @throws 
	* @author leeko
	 */
	public Map<String,Object> imageUpload(String saveUrl,String dirName,List<MultipartFile> fileList){
		String classPath=this.getClass().getClassLoader().getResource("/").getPath();
		String savePath = classPath.replaceAll("WEB-INF/classes/", "");
		savePath=savePath+"attached/";
		Map<String,Object> result=new HashMap<>();
		
		result.put("error", 1);
		
		//文件保存目录URL
		//String saveUrl  = request.getContextPath() + "/attached/";
		//定义允许上传的文件扩展名
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		extMap.put("flash", "swf,flv");
		extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
		extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
		//最大文件大小
		long maxSize = 1000000;
		
		//检查目录
		File uploadDir = new File(savePath);
		if(!uploadDir.isDirectory()){
			uploadDir.mkdirs();
		}
		//检查目录写权限
		if(!uploadDir.canWrite()){
			result.put("message", "上传目录没有写权限。");
			return result;
		}

		
		if(!extMap.containsKey(dirName)){
			result.put("message", "目录名不正确。");
			
			return result;
		}
		//创建文件夹
		savePath += dirName + "/";
		saveUrl += dirName + "/";
		File saveDirFile = new File(savePath);
		if (!saveDirFile.exists()) {
			saveDirFile.mkdirs();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String ymd = sdf.format(new Date());
		savePath += ymd + "/";
		saveUrl += ymd + "/";
		File dirFile = new File(savePath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}

		for (MultipartFile mf : fileList) {  
            if(!mf.isEmpty()){  
            	String fileName = mf.getOriginalFilename();
            	if(mf.getSize() > maxSize){
					result.put("message", "上传文件大小超过限制。");
					return result;
				}

				//检查扩展名
				String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
				if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
					result.put("message", "上传文件格式不被支持。");
					return result;
				}
				
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
				String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
				try{
					
					mf.transferTo(new File(savePath+newFileName));
				}catch(Exception e){
					result.put("message", "上传文件失败");
					return result;
				}
				result.put("error",0);
				result.put("message","上传文件成功");
				result.put("url",saveUrl + newFileName);
            }  
        }  
		return result;
	}
	
	
}
